#!/bin/bash

MASTER_IP=$1
KUBE_VERSION=$2
NODENAME=$(hostname -s)
# SERVICE_CIDR="10.96.0.0/12"
SERVICE_CIDR="10.96.0.0/16"
POD_CIDR="10.244.0.0/16"
COREDNS_VERSION=1.8.7


echo -e "\n\n=========== Start Kubernetes Cluster =========== "
sudo kubeadm init \
  --kubernetes-version=v$KUBE_VERSION \
  --apiserver-advertise-address=$MASTER_IP \
  --image-repository=registry.aliyuncs.com/google_containers \
  --service-cidr=$SERVICE_CIDR \
  --pod-network-cidr=$POD_CIDR \
  --node-name=$NODENAME \
  --ignore-preflight-errors=Swap


echo -e "\n\n=========== Copy Kube Config To Vagrant User .kube Directory =========== "
mkdir /home/vagrant/.kube
cp /etc/kubernetes/admin.conf /home/vagrant/.kube/config
chown -R vagrant:vagrant /home/vagrant/.kube


echo -e "\n\n=========== Generate Join Command To Cluster For Worker Nodes =========== "
kubeadm token create --print-join-command > /join_worker_node.sh
# kmaster也作为一个工作节点参与pod的调度
su - vagrant -c "kubectl taint nodes --all node-role.kubernetes.io/master-"


echo -e "\n\n=========== Install calico =========== "
# 这里注意修改calico.yml文件中的 "IP_AUTODETECTION_METHOD" 的网卡名称
su - vagrant -c "kubectl apply -f /vagrant/calico/calico.yaml"


echo -e "\n\n=========== Install dashboard =========== "
su - vagrant -c "kubectl apply -f /vagrant/kube-dashboard/kube-dashboard.yaml"


echo -e "\n\n=========== 安装nfs ================"
yum install -y rpcbind nfs-utils
mkdir /mnt/storage
chmod 777 -R /mnt
cat >>/etc/exports<<EOF
/mnt/storage *(rw,sync,no_root_squash,no_subtree_check)
EOF
systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server
exportfs -r